package com.example.docker_demo_connect_database.controller;

import com.example.docker_demo_connect_database.dao.IUserDao;
import com.example.docker_demo_connect_database.entity.User;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Log4j2
@RestController
@RequestMapping("")
public class DemoController {

    @Autowired
    IUserDao userDao;

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("")
    public String index(){

        return "hello world";
    }

    @GetMapping("/database/get/{id}")
    public String get(@PathVariable int id){
        log.info("/database/get/{}",id);
        List<User> users = userDao.select(id);
        return Arrays.toString(users.stream().toArray());
    }

    @GetMapping("/database/add/{name}")
    public String add(@PathVariable String name){
        log.info("/database/add/{}",name);
        userDao.insert(1,name);
        return "ok";
    }

    @GetMapping("/redis/add/{key}/{value}")
    public String addKey(@PathVariable String key, @PathVariable String value){
        log.info("/redis/add/{}/{}",key,value);
        redisTemplate.opsForValue().append(key,value);
        return "ok";
    }

    @GetMapping("/redis/get/{key}")
    public String getKey(@PathVariable String key){
        log.info("/redis/get/{}",key);
        String value = redisTemplate.opsForValue().get(key);
        return value;
    }
}
